package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysBasDictTable;
import com.ysd.lis.entity.SysBasDictTableField;
import com.ysd.lis.mapper.sys.SysBasDictTableFieldMapper;
import com.ysd.lis.mapper.sys.SysBasDictTableMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.SysBasDictTableFieldService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2021-10-08
 */
@Service
@Slf4j
public class SysBasDictTableFieldServiceImpl extends ServiceImpl<SysBasDictTableFieldMapper, SysBasDictTableField> implements SysBasDictTableFieldService {
    @Autowired
    SysBasDictTableFieldMapper sysBasDictTableFieldMapper;
    @Autowired
    SysBasDictTableMapper sysBasDictTableMapper;
    @Override
    public Result getDictTableFieldAndTableDataList(CommonPageDto pageParam) {
        Map<String,Object> returnMap = new HashMap<>();
        LambdaQueryWrapper<SysBasDictTableField> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysBasDictTableField::getPid,pageParam.getPid()).eq(SysBasDictTableField::getDelFlag,0);
        queryWrapper.orderByAsc(SysBasDictTableField::getSx);
        List<SysBasDictTableField> tableFieldList = sysBasDictTableFieldMapper.selectList(queryWrapper);
        returnMap.put("tableFieldList",tableFieldList);
        List<String> columNameList = tableFieldList.stream().map(SysBasDictTableField::getColumnName).collect(Collectors.toList());
        List<Map<String, Object>> transmitTodoData = sysBasDictTableMapper.getTransmitTodoData(pageParam.getTableName(),columNameList);
        returnMap.put("tableDataList",transmitTodoData);
        return Result.succ(1, "查询成功", returnMap);
    }

    @Override
    public Result saveBatchTableField(SysBasDictTable table) {
        boolean b = this.saveBatch(table.getFieldList());
        if(b){
            return Result.succ(1, "批量新增成功", "");
        }else {
            return Result.fail(400, "批量新增失败", "");
        }
    }

    @Override
    public Result deleteDictTableField(SysBasDictTableField field) {
        int deleteById = sysBasDictTableFieldMapper.deleteById(field.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", "");
        }else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result editDictTableField(SysBasDictTableField field) {

        int d = sysBasDictTableFieldMapper.updateById(field);

        if (d > 0) {
            return Result.succ(1, "删除成功", field);
        } else {
            return Result.fail("删除失败！");
        }
    }

    @Override
    public Result getTableFieldAndDataList(CommonPageDto pageParam) {
        List<Map<String, Object>> transmitTodoData = sysBasDictTableMapper.getTransmitTodoData(pageParam.getTableName(),pageParam.getColumNameList());
        return Result.succ(1, "查询成功", transmitTodoData);
    }
}
